function buildEmrListByDepId(list,depId, selectedEmrId) {
	
	
	var emrOption = '';
	var gotItems = false;
	var count = 0;	
	for( var index in list) {
		if (list[index]["department_id"] == depId) {
			count++;		
		}
		
	}
	if (selectedEmrId == null || selectedEmrId <0) {
		selectedEmrId = 0;
	} 
		
	if (count > 1) {
		for( var index in list) {
			var emr = list[index];
			if (emr["department_id"] == depId) {		    		    	
		    	gotItems = true;
		    	if (emr['emr_id'] == selectedEmrId) {    		        	
		    		emrOption += "<option value='" + emr['emr_id'] + "'"
			    	+ " emr_id='" + emr['emr_id'] + "'"
			    	+ " dep_id='" + depId + "'"
			    	+ " selected='selected' >" + emr['emr_name'] + "</option>";
		    	} else {
		    		emrOption += "<option value='" + emr['emr_id'] + "'"
			    	+ " emr_id='" + emr['emr_id'] + "'"
			    	+ " dep_id='" + depId + "'"
			    	+ ">" + emr['emr_name'] + "</option>";
		    	}
			}
	    	
		}		
	} else if (count == 1) {
		for( var index in list) {
			var emr = list[index];
			if (emr["department_id"] == depId) {		    		    	
		    	gotItems = true;
		    	emrOption += "<option value='" + emr['emr_id'] + "'"
			    	+ " emr_id='" + emr['emr_id'] + "'"
			    	+ " dep_id='" + depId + "'"
			    	+ " selected='selected' >" + emr['emr_name'] + "</option>";		    	
			}
	    	
		}		
	}	
	
	return emrOption;
	
}
function sortList(listID)
{
   var $dd = $('#' + listID);
	if ($dd.length > 0) { // make sure we found the select we were looking for

    // save the selected value
    var selectedVal = $dd.val();

    // get the options and loop through them
    var $options = $('option', $dd);
    var arrVals = [];
    $options.each(function(){
        // push each option value and text into an array
        arrVals.push({
            val: $(this).val(),
            text: $(this).text()
        });
    });

    // sort the array by the value (change val to text to sort by text instead)
    arrVals.sort(function(a, b){
	    if(a.text>b.text){
	        return 1;
	    }
	    else if (a.text==b.text){
	        return 0;
	    }
	    else {
	        return -1;
	    }
	});


    // loop through the sorted array and set the text/values to the options
    for (var i = 0, l = arrVals.length; i < l; i++) {
        $($options[i]).val(arrVals[i].val).text(arrVals[i].text);
    }

    // set the selected value back
    $dd.val(selectedVal);
    }


} 

function loadEmrAjax(selectedEmr, recordId) {	
	$.ajax ({
		type: 	 "GET",
		url:	 "../GeneralEmr/LoadEmrAjax?set_ajax_view",
		data:     "emrId=" + selectedEmr + "&recordId=" + recordId,
		dataType: 	"json",
		beforeSend:	function() {	
			$('#statusMessage').html('');
			toggleModalAjaxLoader();			
		},
		complete: 	function() {
			toggleModalAjaxLoader();			
		},
		success: function(data) {
			if (data.error == null) {
				$('div#encounterPageContent').html(data.content).show();				
			} else {
				$('#statusMessage').html(data.error);				
			}						
			
		}
	});
}